package com.wlc.base.design.jdkproxy;

import java.lang.reflect.Proxy;

/**
 * @author 王立朝 wanglichao@champion-credit.com
 * @description 测试jdk动态代理
 * @date 2021-09-17 00:10
 */
public class JdkProxyTest {

    public static void main(String[] args) {

        // 被代理的对象
        UserDaoImpl userDaoImpl = new UserDaoImpl();

        JdkProxy jdkProxy = new JdkProxy(userDaoImpl);

        // 通过反射获取到被代理的类的类加载器
        ClassLoader classLoader = userDaoImpl.getClass().getClassLoader();
        // 获取到代理类实现的所有的接口们
        Class<?>[] interfaces = userDaoImpl.getClass().getInterfaces();
        // 主要装载器、一组接口及调用处理动态代理实例

        UserDao userDao1 = (UserDao) Proxy.newProxyInstance(classLoader, interfaces, jdkProxy);
        userDao1.save();
        System.out.println("=======");
        News news = (News) Proxy.newProxyInstance(classLoader, interfaces, jdkProxy);
        news.getNews();


    }
}
